package com.ruoyi.common.utils.uuid;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.VerifyCodeUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.IntStream;

/**
 * ID生成器工具类
 *
 * @author ruoyi
 */
public class IdUtils {

    private static Map<String, String> map = new HashMap<>();

    static {
        // 延期/新卡支付延期
        map.put("walletDeferred", "51");
        map.put("deferred", "50");
        // 还款/新卡支付还款
        map.put("repay", "40");
        // 绑卡
        map.put("bindcard", "41");
        // 贷款
        map.put("loan", "10");
        // 退款
        map.put("refund", "20");
        // 提现
        map.put("withdraw", "30");
    }

    /**
     * 获取随机UUID
     *
     * @return 随机UUID
     */
    public static String randomUUID() {
        return UUID.randomUUID().toString();
    }

    /**
     * 简化的UUID，去掉了横线
     *
     * @return 简化的UUID，去掉了横线
     */
    public static String simpleUUID() {
        return UUID.randomUUID().toString(true);
    }

    /**
     * 获取随机UUID，使用性能更好的ThreadLocalRandom生成UUID
     *
     * @return 随机UUID
     */
    public static String fastUUID() {
        return UUID.fastUUID().toString();
    }

    /**
     * 简化的UUID，去掉了横线，使用性能更好的ThreadLocalRandom生成UUID
     *
     * @return 简化的UUID，去掉了横线
     */
    public static String fastSimpleUUID() {
        return UUID.fastUUID().toString(true);
    }

    public static String reqId() {
        //return DateUtils.dateTimeNow() + String.valueOf(SnowflakeIdWorker.getIdWorkerInstance().nextId()).substring(7);
        return DateUtils.dateTimeNow() + String.valueOf(IdWorker.getIdStr()).substring(7);
    }

    public static String serialNo(String type) {
        return DateUtils.dateTimeNow() + String.valueOf(IdWorker.getIdStr()).substring(7) + Constants.SERIALNO_SPLIT + map.get(type);
    }

    public static String generateCustNo() {
        return DateUtils.dateTime() + String.valueOf(IdWorker.getIdStr()).substring(7);
    }

    /**
     * 生成合同编号
     *
     * @return
     */
    public static String generateContractNo() {
        String str = DateUtils.dateTimeNow(DateUtils.YYYYMMDD) + VerifyCodeUtils.generateVerifyCode(6) + String.valueOf(IdWorker.getIdStr()).substring(12);
        return str;
    }

}
